<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2018/7/31
 * Time: 22:17
 */

namespace app\admin\controller;
use app\admin\model\WechatUser;
use think\Db;
use app\home\model\Write;
/**
 * Class Play
 * @package app\admin\controller
 * 私人订制
 */
class Play extends Admin
{
    /**
     * @return mixed
     * 学校列表
     */
    public function school(){
        $school = input('school');
        if (empty($school)){
            $map = ['dq_id' => ['neq',0]];
        }else{
            $map = ['dq_id' => ['neq',0] ,'dx' => ['like','%'.$school.'%']];
        }
        $list = Db::table('xuexiao')
            ->where($map)
            ->order('dx','asc')
            ->paginate(10,false,['query' => ['school' => $school]]);
        $this->assign('list',$list);
        return $this->fetch();
    }
    /*
     * 学员列表页面
     */
    public function index(){
        $id = input('id');  // 学校id
        $vip = input('vip');
        $type = input('type');
        if (substr($vip,0,1) === "@"){
            $vipp = substr($vip,1);
            $map = ['a.review_status' => 1, 'school_id' => $id,'a.role' => 0,'a.name' => ['like', "%$vipp%"]];
        }else{
            $map = ['a.review_status' => 1, 'school_id' => $id,'a.role' => 0,'a.order_id' => ['like', "%$vip%"]];
        }
        if (empty($type) || $type == 1){  // 全部
            $list = Db::table('pb_wechat_user')
                ->alias('a')
                ->where($map)
                ->field('a.openid,a.id,a.name,a.school_id,a.order_id,a.is_sign')
                ->order('a.school_id,a.order_id','desc')
                ->paginate(10,false,['query' => ['vip' => $vip]]);
        }elseif ($type == 2){  // 已分配
            $map['is_sign'] = 1;
            $list = Db::table('pb_wechat_user')
                ->alias('a')
                ->field('a.openid,a.id,a.name,a.school_id,a.order_id,a.is_sign')
                ->where($map)
                ->order('a.school_id,a.order_id','desc')
                ->paginate(10,false,['query' => ['vip' => $vip]]);
        }elseif ($type == 3){  // 未分配
            $map['is_sign'] = 0;
            $list = Db::table('pb_wechat_user')
                ->alias('a')
                ->where($map)
                ->field('a.openid,a.id,a.name,a.school_id,a.order_id,a.is_sign')
                ->order('a.school_id,a.order_id','desc')
                ->paginate(10,false,['query' => ['vip' => $vip]]);
        }
        $this->assign('list',$list);
        $this->assign('type',$type);
        $this->assign('id',$id);
        return $this->fetch();
    }
    /**
     * 操作
     */
    public function play(){
        $data = input('post.');
        $users = $data['users']; // 数组
        $type = $data['type']; // 1 确认 0 取消
        foreach ($users as $value){
            WechatUser::where(['openid' => $value])->update(['is_sign' => $type]);
        }
        return $this->success('操作成功');
    }

    /**
     * @return mixed
     * 预览
     */
    public function preview(){
        $Model = new Write();
        $id = input('id');
        $User = WechatUser::where(['id' => $id])->field('openid,name')->find();
        $openid = $User['openid'];
        $list = $Model::get(['openid' => $openid]);
        if ($list['qqqs']){
            $list['qqqs'] = json_decode($list['qqqs']);
        }
        $list['user'] = $User['name'];
        $this->assign('info',$list);
        return $this->fetch();
    }
    /*
     * 批阅记录
     */
    public function paper(){
        $openid = input('openid');
        $list = Db::name('writeSign')
            ->where(['openid' => $openid])
            ->order('create_time','desc')
            ->paginate(10);
        $this->assign('list',$list);
        return $this->fetch();
    }
    /*
     * 删除
     */
    public function del(){
        $id = input('id');
        if (empty($id)){
            return $this->error('系统参数错误');
        }
        $res = Db::name('writeSign')->where('id',$id)->delete();
        if ($res){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }
    /**
     * 添加
     */
    public function edit(){
        $Model = new Write();
        if (IS_POST){
            $data = input('post.');
            if ($data['qqqs'])$data['qqqs'] = json_encode($data['qqqs']);
            $data['time'] = strtotime($data['time']);
            $data['status'] = 1;
            $data['create_time'] = time();
            if ($data['id']){
                // 修改
                $res = $Model->save($data,['id' => $data['id']]);
            }else{
                // 添加
                unset($data['id']);
                $res = $Model->save($data);
            }
            if ($res){
                return $this->success('操作成功');
            }else{
                return $this->error('操作失败');
            }
        }else{
            $openid = input('openid');
            $info = [];
            $res = $Model->where(['openid' => $openid])->find();
            $User = WechatUser::where(['openid' => $openid])->field('name,tel,order_id')->find();
            $this->assign('na',$User['name']);
            $this->assign('tel',$User['tel']);
            $this->assign('vip',$User['order_id']);
            // 分校
            $place = Db::name('fenxiao')->field('num,name')->select();
            $this->assign('place',$place);
            // 班主任
            $name = Db::name('xxbteacher')->group('name')->order('name','asc')->select();
            $this->assign('name',$name);
            // 考试年份
            $y = date('Y')-1;
            $year = [];
            for ($i=0;$i<=4;$i++){
                array_push($year,$y+$i);
            }
            $this->assign('year',$year);
            // 项目
            $project = Db::name('banxing')->field('xm')->group('xm')->order('xm','desc')->select();
            $this->assign('pro',$project);
            // 报班类型
            $ban = Db::name('banxing')->where('xm','论文')->order('id','asc')->select();
            $this->assign('ban',$ban);
            // 入学年份
            $yy = date('Y')-5;
            $sch = [];
            for ($i=0;$i<=9;$i++){
                array_push($sch,$yy+$i);
            }
            $this->assign('sch',$sch);
            // 学校地区  所属
            $area = Db::name('diqu')->order('dq_id','asc')->select();
            foreach ($area as $key => $value){
                $area[$key]['val'] = $value['diqu'].' '.$value['qu'];
            }
            $this->assign('area',$area);
            // 学校地区 目标
            $this->assign('areas',$area);
            // 学校  所属
            $school = Db::table('xuexiao')->where('dq_id',12)->order('dx','asc')->select();
            $this->assign('school',$school);
            // 学校  目标
            $this->assign('schools',$school);
            // 学院  所属
            $home = Db::name('yxzy')->where('school_id',439)->group('xy')->select();
            $this->assign('home',$home);
            // 学院  目标
            $this->assign('homes',$home);
            // 专业 所属
            $zy = Db::name('yxzy')->where(['school_id' => 439,'xy' => '电子信息学院'])->order('zy','asc')->select();
            $this->assign('zy',$zy);
            // 专业 目标表
            $this->assign('zys',$zy);
            // 抄送给 老师
            $teacher = WechatUser::where(['review_status' => 1,'role' => 1,'subscribe' => 1])->field('name,openid')->order('name')->select();
            $this->assign('teacher',$teacher);
            if ($res){
                $res['qqqs'] = json_decode($res['qqqs']);
                $Re = $this->sel(5,$res['qqq']);
                $res['qqqss'] = $Re['data'];
                // 报班类型
                $ban = Db::name('banxing')->where('xm',$res['bbxm'])->order('id','asc')->select();
                $this->assign('ban',$ban);
                // 根据地区 得学校  所属
                $arr = explode(' ',$res['xx_dq']);
                $id = Db::name('diqu')->where(['diqu' => $arr[0],'qu' => $arr[1]])->value('dq_id');
                $school = Db::table('xuexiao')->where('dq_id',$id)->field('dx,dx_id')->order('dx','asc')->select();
                $this->assign('school',$school);
                // 根据地区 得学校  目标
                $arrs = explode(' ',$res['mb_dq']);
                $ids = Db::name('diqu')->where(['diqu' => $arrs[0],'qu' => $arrs[1]])->value('dq_id');
                $schools = Db::table('xuexiao')->where('dq_id',$ids)->field('dx,dx_id')->order('dx','asc')->select();
                $this->assign('schools',$schools);
                // 根据学校 得学院 所属
                $id = Db::table('xuexiao')->where('dx',$res['xx_mc'])->value('dx_id');
                $home = Db::table('pb_yxzy')->where('school_id',$id)->group('xy')->select();
                $this->assign('home',$home);
                // 根据学校 得学院 目标
                $ids = Db::table('xuexiao')->where('dx',$res['mb_mc'])->value('dx_id');
                $homes = Db::table('pb_yxzy')->where('school_id',$ids)->group('xy')->select();
                $this->assign('homes',$homes);
                // 根据 学院 得专业  所属
                $zy = Db::table('pb_yxzy')->where(['school_id' => $id,'xy' => $res['xx_xy']])->order('zy','asc')->select();
                $this->assign('zy',$zy);
                // 根据 学院 得专业  目标
                $zys = Db::table('pb_yxzy')->where(['school_id' => $ids,'xy' => $res['mb_xy']])->order('zy','asc')->select();
                $this->assign('zys',$zys);
                $info = $res;
                
            }
            $this->assign('openid',$openid);
            $this->assign('msg',$info);
            return $this->fetch();
        }
    }
    /**
     * 类型选择
     */
    public function sel($type=0,$name='',$names=''){
        $type = input('type') ? input('type') : $type;
        $name = input('name') ? input('name') : $name;
        $info = [];
        switch ($type){
            case 1:
                // 报班类型
                $info = Db::name('banxing')->where('xm',$name)->order('id','asc')->select();
                break;
            case 2:
                $arr = explode(' ',$name);
                $id = Db::name('diqu')->where(['diqu' => $arr[0],'qu' => $arr[1]])->value('dq_id');
                // 地区
                $info = Db::table('xuexiao')->where('dq_id',$id)->field('dx,dx_id')->order('dx','asc')->select();
                break;
            case 3:
                // 学院
                $id = Db::table('xuexiao')->where('dx',$name)->value('dx_id');
                $info = Db::table('pb_yxzy')->where('school_id',$id)->group('xy')->select();
                break;
            case 4:
                // 专业
                $names = input('names') ? input('names') : $names; // 学校名称
                $id = Db::table('xuexiao')->where('dx',$names)->value('dx_id');
                $info = Db::table('pb_yxzy')->where(['school_id' => $id,'xy' => $name])->order('zy','asc')->select();
                break;
            case 5:
                // qq群
                switch ($name){
                    case '考研':
                        $info = ['考研同年级VIP学员群','英语VIP精听精翻群','数学答疑群','点对点政治群','VIP论文群','431金融学综合','199/396VIP学员群','西方经济学VIP学员群','法硕VIP群','分校学员群'];
                        break;
                    case '专升本':
                        $info = ['专升本同年级VIP学员群','英语VIP精听群','数学答疑群','语文答疑群','分校学员群'];
                        break;
                    case '口译':
                        $info = ['口译学员总群','当期口译笔试群','当期口译口试群','分校学员群'];
                        break;
                    case '雅思':
                        $info = ['雅思学员总群','分校学员群'];
                        break;
                    case '计算机':
                        $info = ['计算机学员总群','分校学员群'];
                        break;
                    case '论文':
                        $info = ['VIP学员群','论文准备微信群','当期论文群'];
                        break;
                }
                break;
        }
        return ['code' => 1,'msg' => '成功','data' => $info];
    }
}